package com.gitee.qdbp.general.common.biz.settings.service;

import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.result.ResultCode;
import com.gitee.qdbp.base.system.model.IAccount;
import com.gitee.qdbp.general.common.api.settings.basic.ISensitiveWordBasic;
import com.gitee.qdbp.general.common.api.settings.model.SensitiveWordBean;
import com.gitee.qdbp.general.common.api.settings.model.SensitiveWordUpdate;
import com.gitee.qdbp.general.common.api.settings.service.ISensitiveWordExecutor;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 敏感词信息业务处理类
 *
 * @author zhh
 * @version 170802
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public class SensitiveWordExecutor implements ISensitiveWordExecutor {

    /** 日志对象 **/
    private static final Logger log = LoggerFactory.getLogger(SensitiveWordExecutor.class);

    /** 敏感词信息DAO **/
    @Autowired
    private ISensitiveWordBasic sensitiveWordBasic;

    @Override
    public void create(SensitiveWordBean model, IAccount me) throws ServiceException {
        String msg = "Failed to create SensitiveWord. ";

        if (VerifyTools.isBlank(me)) {
            log.error(msg + "params is null: me");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(model)) {
            log.error(msg + "params is null: model");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // TODO: 参数验证
        // if (VerifyTools.isBlank(model.getXxx())) {
        //     log.error(msg + "params is null: xxx");
        //     throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        // }

        // 业务逻辑
        model.setId(null); // 主键由Basic生成, 不允许外部传入
        // TODO: 补充其他业务逻辑
        // 设置创建人信息
        // model.setCreatorId(me.getUserId()); // 创建人ID
        // model.setCreatorName(me.getDisplayName()); // 创建人姓名

        // 向gn_sensitive_word表插入记录
        sensitiveWordBasic.create(model);

        throw new UnsupportedOperationException("该方法的业务逻辑暂未实现"); // TODO 完成业务逻辑的实现之后删除此行
    }

    @Override
    public void update(SensitiveWordUpdate model, IAccount me) throws ServiceException {
        String msg = "Failed to update SensitiveWord. ";

        if (VerifyTools.isBlank(me)) {
            log.error(msg + "params is null: me");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(model)) {
            log.error(msg + "params is null: model");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // TODO: 参数验证
        // if (VerifyTools.isBlank(model.getXxx())) {
        //     log.error(msg + "params is null: Xxx");
        //     throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        // }

        String id = model.getId();
        if (VerifyTools.isBlank(id) && model.getWhere() != null) {
            id = model.getWhere().getId();
        }
        if (VerifyTools.isBlank(id)) {
            log.error(msg + "params is null: id");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 原始记录检查
        SensitiveWordBean older = sensitiveWordBasic.findById(id);
        if (older == null) {
            log.error(msg + "record is not found. id=" + id);
            throw new ServiceException(ResultCode.RECORD_NOT_EXIST);
        }

        // TODO: 业务逻辑
        // 判断哪些状态的记录不允许修改...
        // if (VerifyTools.isExists(older.getState(), State.DISABLED, State.Xxx)) {
        //     throw new ServiceException(ResultCode.RECORD_STATE_ERROR);
        // }

        SensitiveWordUpdate newer = createChangedModel(model, older);

        if (newer != null) {
            // 更新gn_sensitive_word表的记录
            sensitiveWordBasic.update(newer, false);
        }

        throw new UnsupportedOperationException("该方法的业务逻辑暂未实现"); // TODO 完成业务逻辑的实现之后删除此行
    }

    @Override
    public void delete(String id, IAccount me) throws ServiceException {
        String msg = "Failed to delete SensitiveWord. ";

        if (VerifyTools.isBlank(me)) {
            log.error(msg + "params is null: me");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(id)) {
            log.error(msg + "params is null: id");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 从gn_sensitive_word表查询原记录
        SensitiveWordBean older = sensitiveWordBasic.findById(id);
        if (older == null) {
            log.error(msg + "record is not found.\n\tid is " + id);
            throw new ServiceException(ResultCode.RECORD_NOT_EXIST);
        }

        // TODO: 业务逻辑
        // 判断哪些状态的记录不允许删除...
        // if (VerifyTools.isExists(older.getState(), State.DISABLED, State.Xxx)) {
        //     throw new ServiceException(ResultCode.RECORD_STATE_ERROR);
        // }

        // 删除gn_sensitive_word表的记录
        sensitiveWordBasic.deleteByIds(Arrays.asList(id), false);

        throw new UnsupportedOperationException("该方法的业务逻辑暂未实现"); // TODO 完成业务逻辑的实现之后删除此行
    }

    @Override
    public void delete(List<String> ids, IAccount me) throws ServiceException {
        String msg = "Failed to delete SensitiveWord. ";

        if (VerifyTools.isBlank(me)) {
            log.error(msg + "params is null: me");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(ids)) {
            log.error(msg + "params is null: ids");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 删除gn_sensitive_word表的记录
        sensitiveWordBasic.deleteByIds(ids, false);

        throw new UnsupportedOperationException("该方法的业务逻辑暂未实现"); // TODO 完成业务逻辑的实现之后删除此行
    }

    /**
     * 创建一个新的对象, 只包含改动过的字段
     *
     * @author zhh
     * @param model 目标对象(一般是参数传入的)
     * @param older 源对象(一般是从数据库查询得到的)
     * @return 只包含有更新的字段对象
     */
    private SensitiveWordUpdate createChangedModel(SensitiveWordUpdate model, SensitiveWordBean older) {
        // SensitiveWordUpdate newer = new SensitiveWordUpdate();
        // newer.setWhere(model.getWhere());
        //
        // boolean changed = false;
        // newer.setId(model.getId()); // 敏感词ID
        //
        // if (VerifyTools.isChanged(model.getTenantCode(), older.getTenantCode())) {
        //     changed = true;
        //     newer.setTenantCode(model.getTenantCode()); // 租户编号
        // }
        // if (VerifyTools.isChanged(model.getType(), older.getType())) {
        //     changed = true;
        //     newer.setType(model.getType()); // 敏感词类型(0.默认)
        // }
        // if (VerifyTools.isChanged(model.getWord(), older.getWord())) {
        //     changed = true;
        //     newer.setWord(model.getWord()); // 敏感词
        // }
        // if (VerifyTools.isChanged(model.getLevel(), older.getLevel())) {
        //     changed = true;
        //     newer.setLevel(model.getLevel()); // 敏感级别(1.禁止|2.警告)
        // }
        // if (VerifyTools.isChanged(model.getExcludeList(), older.getExcludeList())) {
        //     changed = true;
        //     newer.setExcludeList(model.getExcludeList()); // 例外组合
        // }
        // if (VerifyTools.isChanged(model.getCreateTime(), older.getCreateTime())) {
        //     changed = true;
        //     newer.setCreateTime(model.getCreateTime()); // 创建时间
        // }
        // if (VerifyTools.isChanged(model.getUsingState(), older.getUsingState())) {
        //     changed = true;
        //     newer.setUsingState(model.getUsingState()); // 启用状态(1.正常|2.未启用)
        // }
        // if (Boolean.TRUE.equals(model.isTenantCodeToNull())) {
        //     changed = true;
        //     newer.setTenantCodeToNull(true); // 租户编号更新为空值
        // }
        // if (Boolean.TRUE.equals(model.isTypeToNull())) {
        //     changed = true;
        //     newer.setTypeToNull(true); // 敏感词类型(0.默认)更新为空值
        // }
        // if (Boolean.TRUE.equals(model.isWordToNull())) {
        //     changed = true;
        //     newer.setWordToNull(true); // 敏感词更新为空值
        // }
        // if (Boolean.TRUE.equals(model.isLevelToNull())) {
        //     changed = true;
        //     newer.setLevelToNull(true); // 敏感级别(1.禁止|2.警告)更新为空值
        // }
        // if (Boolean.TRUE.equals(model.isExcludeListToNull())) {
        //     changed = true;
        //     newer.setExcludeListToNull(true); // 例外组合更新为空值
        // }
        // if (Boolean.TRUE.equals(model.isUsingStateToNull())) {
        //     changed = true;
        //     newer.setUsingStateToNull(true); // 启用状态(1.正常|2.未启用)更新为空值
        // }
        // return changed ? newer : null;

        throw new UnsupportedOperationException("请确认SensitiveWordUpdate哪些字段是允许修改的"); // TODO 完成业务逻辑的实现之后删除此行
    }

}